import Vue from 'vue';
import Vuex from 'vuex';
Vue.use(Vuex);

const store = new Vuex.Store({
    state: {
        affixList: [],
        affixOptions: {},
        cache: {},
        compare_list: [],
        calc_list: [],
        favorited_list: [],
    },

    mutations: {
        //@BUG 直接对state.xx对象的某个属性、数组对象 修改导致检测不到变动，
        //必须对state.xx重新赋值才可以  先随便赋个值 在变回来
        //注意不能用克隆法，导致数组中对象不是原来的对象了

        //key_value   {key:'',value:Object} 一次只能传一组
        setCache(state, key_value) {
            state.cache[key_value.key] = key_value.value;

            let old = state.cache;
            state.cache = {};
            state.cache = old;
        },

        setAffixList(state, array) {
            state.affixList = array;

            state.affixOptions = {};
            state.affixList.forEach(e => {
                state.affixOptions[e.name] = { ...e }
            });
        },

        //添加元素
        pushComperList(state, object) {
            state.compare_list.push(object);

            let old = state.compare_list;
            state.compare_list = [];
            state.compare_list = old;
        },

        //删除元素 index
        spliceCompareList(state, i) {
            state.compare_list.splice(i, 1);

            let old = state.compare_list;
            state.compare_list = [];
            state.compare_list = old;
        },

        //删除元素 object
        spliceItemCompareList(state, item) {
            state.compare_list.some((e, index) => {
                if (e == item) {
                    console.log("找到对象了")
                    state.compare_list.splice(index, 1);
                    return true;
                }
            });

            let old = state.compare_list;
            state.compare_list = [];
            state.compare_list = old;
        },

        //清空元素
        clearCompareList(state) {
            state.compare_list = []
        },

        //设置某个元素的位置
        //@param loc 1置顶 -1置末尾
        setLocCompareList(state, { item, loc }) {
            state.compare_list.some((e, index) => {
                if (e == item) {
                    console.log("找到对象了")
                    if (loc == -1) {
                        state.compare_list.splice(index, 1);
                        state.compare_list.push(item);

                    } else if (loc == 1) {
                        state.compare_list.splice(index, 1);
                        state.compare_list.splice(0, 0, item);
                    }
                    return true;
                }
            });

            let old = state.compare_list;
            state.compare_list = [];
            state.compare_list = old;
        },

        syncCalceList(state, array) {
            if (array instanceof Array) {
                state.calc_list = array;
                return;
            }
            console.log("$stroe", "syncCalceList", "同步对象不是数组！");
        },

        clearCalceList(state) {
            state.calc_list = [];
        },

        syncFavoritedList(state, array) {
            if (array instanceof Array) {
                state.favorited_list = array;
                return;
            }
            console.log("$stroe", "syncCalceList", "同步对象不是数组！");
        },

        pushFavoritedList(state, item) {
            //检查id是否重复
            console.log("pushFavoritedList item", item);

            if (state.favorited_list.some(e => {
                if (e.id == item.id) {
                    //重复了
                    return true;
                }
            })) {

                console.log("重复了 跳过添加");
                return;
            }

            state.favorited_list.push(item);
            let old = state.favorited_list;
            state.favorited_list = [];
            state.favorited_list = old;
        },

        clearFavoritedList(state) {
            state.favorited_list = [];
        }
    },
});

export default store;